package org.jeecg.modules.yc.util;

/**
 * 数据类型转换类
 *
 * @author 14284
 *
 */
public class TransformationTool {
    /**
     * 16进制String转化为16进制byte[]
     *
     * @param src       字符串
     * @param order     0正序 1倒序
     * @param calculate 计算（减33、加33）
     * @return
     */

    public static byte[] hexStringBytes(String src, int order, int calculate) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        int sub = 0;
        for (int i = 0; i < l; i++) {
            if (order == 1) {
                sub = (l - 1) - i;
            } else {
                sub = i;
            }
            ret[sub] = (byte) ((Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16) + calculate) & 0xff);
        }
        return ret;
    }

    /**
     * 10进制String转化为16进制String
     *
     * @param src   字符串
     * @param times 乘以倍数
     * @return
     */

    public static String decStringHexString(String src, double times) {
        // 转int
        int dec = (int) (Integer.parseInt(src) * times);
        // 转16进制
        src = Integer.toHexString(dec);
        return src;
    }

    public static void main(String[] args) {
        String meterStr = "123456";
        String meterStr1 = meterStr.substring(0,2);
        String meterStr2 = meterStr.substring(2,4);
        String meterStr3 = meterStr.substring(4,6);
        System.out.println(addZeroForNum(decStringHexString(meterStr1,1),2));
        System.out.println(addZeroForNum(decStringHexString(meterStr2,1),2));
        System.out.println(addZeroForNum(decStringHexString(meterStr3,1),2));
        byte[] bytes=TransformationTool.hexStringBytes("0102",1,0);
        String s=TransformationTool.bytesHexString(bytes);
        System.out.println(s);
    }
    /**
     * 16进制byte[]转化为16进制String
     *
     * @param b byte[]
     * @return String
     */
    public static String bytesHexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            ret += hex;
        }
        return ret.toUpperCase();
    }

    /**
     * 补齐字符长度，以'0'前补齐
     *
     * @param str
     * @param strLength
     * @return
     */
    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            sb.append("0").append(str);// 左(前)补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }

    /**
     * int[]转16进制字符串
     *
     * @return
     */
    public static String intsHexString(int[] b) {
        String str = "";
        for (int i = 0; i < b.length; i++) {
            String m = Integer.toHexString(b[i]);
            if (m.length() == 1) {
                m = "0" + m;
            }
            str += m;
        }
        return str.toUpperCase();
    }

    /**
     * int[]转16进制字符串，倒序减0x33
     *
     * @return
     */
    public static String intsReverseMinus33HexString(int[] b) {
        String str = "";
        // 倒序
        for (int i = b.length - 1; i >= 0; i--) {
            // 减0x33
            String m = Integer.toHexString((b[i] - (0x33 & 0xFF)) & 0xff);
            if (m.length() == 1) {
                m = "0" + m;
            }
            str += m;

        }
        return str.toUpperCase();
    }

    /**
     * int数组转byte数组
     *
     * @param msg
     * @return
     */
    public static byte[] intsToBytes(int[] msg) {
        byte[] re = new byte[msg.length];
        for (int i = 0; i < msg.length; i++) {
            // int强转byte
            re[i] = (byte) msg[i];
        }
        return re;
    }

    /**
     * 校验和
     * @param data 校验内容
     * @return 校验和
     */
    public static String checkSum(String data){
        byte[] bytes = hexStringBytes(data,0, 0);
        byte checkSum = 0x00;
        for(byte by : bytes){
            checkSum += by;
        }
        String hex = Integer.toHexString(checkSum & 0xFF);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }
}

